package com.pro.mybatis;

public class SearchCondition {
	private ListPage page;
	private Order order;
	private boolean flag = false;
	private String value = "";
	private GroupBy group;

	private String sql;
	public String getSql() {
		if(sql==null){
			return "*";
		}
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public ListPage getPage() {
		return page;
	}

	public Order getOrder() {
		return order;
	}

	// 并且
	public String and(MyBatisExpression Ex) {
		if (Ex == null || Ex.equals("")) {
			return "";
		}
		if (flag) {
			value += " and ";
		} else {
			flag = true;
		}
		value += Ex.toString();
		return value;
	}

	// 或者
	public String or(MyBatisExpression Ex) {
		value += " or ";
		value += Ex.toString();
		return value;

	}

	public String and(String Ex) {
		if (Ex == null || Ex.equals("")) {
			return "";
		}
		if (flag) {
			value += " and ";
		} else {
			flag = true;
		}
		value += Ex.toString();
		return value;
	}

	// 或者
	public String or(String Ex) {
		if (Ex == null || Ex.equals("")) {
			return "";
		}
		value += " or ";
		value += Ex.toString();
		return value;

	}

	public void setOrder(Order order) {
		// return "";
		this.order = order;
	}

	public void setGroupBy(GroupBy group) {
		this.group = group;
	}

	public void setPage(ListPage page) {
		this.page = page;
	}

	public String toString(boolean flag) {
		if (value != null && !value.equals("") &&value.indexOf("where")==-1) {
			value = " where " + value;
		}
		if (group != null) {
			value += group;
		}
		if (order != null) {
			value += order;
		}
		if (page != null && flag) {
			value += page;
		}
		return value;
	}

	public String getString() {
		return this.toString(true);
	}

	public String getStringWithoutPage() {
		return this.toString(false);
	}
}